/*
 * @(#)GetFoldersForDocumentRunner.java
 * Date 2013-09-10
 * Version 1.0
 * Author Jim Horner
 * Copyright (c)2012
 */


package us.hornerscorners.lamppost.registry.query.es;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;

import us.hornerscorners.lamppost.exception.ServiceActionException;

import us.hornerscorners.lamppost.ihecommon.config.IHEConfig;

import us.hornerscorners.lamppost.ihecommon.service.ElasticSearchSource;

import us.hornerscorners.lamppost.model.common.PersistableModelType;

import us.hornerscorners.lamppost.model.document.Association;
import us.hornerscorners.lamppost.model.document.DocumentEntry;

import us.hornerscorners.lamppost.registry.constant.QueryField;

import us.hornerscorners.lamppost.registry.model.QueryRequest;
import us.hornerscorners.lamppost.registry.model.QueryResponse;
import us.hornerscorners.lamppost.registry.model.RegistryObjects;

import us.hornerscorners.lamppost.registry.transformer.model.CodedElementTransformer;
import us.hornerscorners.lamppost.registry.transformer.model.PatientIdentifierTransformer;


/**
 * Class description
 *
 *
 * @version        v1.0, 2013-09-10
 * @author         Jim Horner
 */
public class GetFoldersForDocumentRunner extends GetDocumentsRunner {

    /**
     * Constructs ...
     *
     *
     * @param config
     * @param elasticSearchSource
     * @param codedElementTransformer
     * @param patientIdentifierTransformer
     */
    public GetFoldersForDocumentRunner(
            IHEConfig config, ElasticSearchSource elasticSearchSource,
            CodedElementTransformer codedElementTransformer,
            PatientIdentifierTransformer patientIdentifierTransformer) {

        super(config, elasticSearchSource, codedElementTransformer,
              patientIdentifierTransformer);
    }

    /**
     *  Method description
     *
     *
     *
     *  @return
     */
    public Map<PersistableModelType,
               QueryBuilder> createAssociationsQuery(Collection<String> folderEntryURNs) {

        Map<PersistableModelType, QueryBuilder> result = new HashMap<>();

        // Folders
        BoolQueryBuilder folderQuery = QueryBuilders.boolQuery();

        result.put(PersistableModelType.Folder, folderQuery);

        folderQuery.must(createStringQuery(false, folderEntryURNs,
                                           QueryField.FolderEntryUUID));

        return result;
    }

    /**
     * Method description
     *
     *
     * @param request
     *
     * @return
     *
     * @throws ServiceActionException
     */
    @Override
    public QueryResponse run(QueryRequest request) throws ServiceActionException {

        // find all the documents
        QueryResponse result = super.run(request);

        // need to run a second query to get associations
        List<String> folderEntryURNs = new ArrayList<>();

        RegistryObjects rol = result.getRegistryObjects();

        for (DocumentEntry docEntry : rol.getDocumentEntries()) {

            for (Association assoc : docEntry.getFolders()) {

                folderEntryURNs.add(assoc.getSourceEntryURN());
            }
        }

        rol.addAll(runQueries(createAssociationsQuery(folderEntryURNs)));

        return result;
    }
}
